package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.stadium.api.req.*;
import com.quanyan.stadium.api.req.gameEvent.ReqGameEventSearch;
import com.quanyan.stadium.api.req.gameEventConfig.ReqClubCreateGameEvent;
import com.quanyan.stadium.api.req.v2.ReqUpdateOpponent;
import com.quanyan.stadium.api.resp.RespGameEventData;
import com.quanyan.stadium.api.resp.RespGameEventDetail;
import com.quanyan.stadium.api.resp.RespOfficialGameEvent;
import com.quanyan.stadium.api.resp.RespOfficialGameTeam;
import com.quanyan.stadium.api.resp.v2.*;
import com.quanyan.stadium.entity.vo.GameEvent;
import com.quanyan.stadium.entity.vo.GameRound;
import com.quanyan.stadium.entity.vo.GameSchedule;
import com.quanyan.stadium.entity.vo.GameTeam;
import com.quanyan.stadium.enums.GameSystemConfigEnum;
import com.quanyan.stadium.service.GameEventService;
import com.quanyan.stadium.service.commonComponent.VersionControlService;
import com.quanyan.stadium.service.gameEvent.ClubGameEventConfigService;
import com.quanyan.stadium.service.gameEvent.GameEventSearchService;
import com.quanyan.stadium.service.gameEvent.LiveArrangeGameEventService;
import com.quanyan.stadium.service.gameRules.GameRoundService;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;

/**
 * 赛事:赛事创建\赛事查询\赛事修改
 *
 * */
@RestController("apiGameEvent")
@RequestMapping("/api/stadium/gameEvent")
public class GameEventController {

    private final static Logger logger = LoggerFactory.getLogger(GameEventController.class);

    @Autowired
    private GameEventService gameEventService;

    @Autowired
    private ClubGameEventConfigService clubGameEventConfigService;

    @Autowired
    private GameEventSearchService gameEventSearchService;

    @Autowired
    private GameRoundService gameRoundService;

    @Autowired
    private LiveArrangeGameEventService liveArrangeGameEventService;

    @Autowired
    private VersionControlService versionControlService;

    /**
     * 赛事编辑----轮次完结
     *
     * */
    @RequestMapping(value="/finishGameRound", method = RequestMethod.POST)
    public APIResponse<?> finishGameRound(@RequestBody @Valid ReqFinishGameEvent reqFinishGameEvent, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.finishGameRound(reqFinishGameEvent, (Integer) request.getAttribute("uid"));
    }

    /**
     * 赛事编辑----赛事完结
     *
     * */
    @RequestMapping(value="/finishGameEvent", method = RequestMethod.POST)
    public APIResponse<?> finishGameEvent(@RequestBody @Valid ReqFinishGameEvent reqFinishGameEvent, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.finishGameEvent(reqFinishGameEvent, (Integer) request.getAttribute("uid"));
    }

    /**
     * 赛事编辑----赛事取消
     *
     * */
    @RequestMapping(value="/cancelGameEvent", method = RequestMethod.POST)
    public APIResponse<?> cancelGameEvent(@RequestBody @Valid ReqId reqId, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.cancelClubGameEvent(reqId.getGameEventId());
    }

    /**
     * 赛事编辑----获取俱乐部绑定场馆
     *
     * */
    @RequestMapping(value="/getClubPlaceGameEventList", method = RequestMethod.POST)
    public APIResponse<List<GameEvent>> getClubPlaceGameEventList(@RequestBody @Valid ReqId reqId, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getClubId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getCategoryId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.getClubPlaceGameEventList(reqId.getClubId(), reqId.getDistrictId(), reqId.getCategoryId());
    }

    /**
     * 赛事编辑----存在分组的赛事,获取分组数目和晋级数目,根据队伍数目
     *
     * */
    @RequestMapping(value="/getGroupNmAndRankNumByTeamNum", method = RequestMethod.POST)
    public APIResponse<?> getGroupNmAndRankNumByTeamNum(@RequestBody @Valid ReqId reqId, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getTeamNum()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (reqId.getTeamNum()<5) {
            return APIResponse.returnFail("队伍数目过少(至少5个队伍),无法分组");
        }
        return APIResponse.returnSuccess(gameRoundService.getGroupNmAndRankNumByTeamNum(reqId.getTeamNum()));
    }



    /**
     * 赛事编辑----创建积分赛事
     *
     * */
    @RequestMapping(value="/createIntegrateGame", method = RequestMethod.POST)
    public APIResponse<?> createIntegrateGame(@RequestBody @Valid ReqCreateClubGameEvent event, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.clubCreateGameEvent((Integer) request.getAttribute("uid"), event);
    }

    /**
     * 赛事编辑-----获取某栏目下的可用赛制类别和玩法类别
     *
     * */
    @RequestMapping(value="/gameCategoryAndSystem", method = RequestMethod.POST)
    public APIResponse<?> gameCategoryAndSystem(@RequestBody ReqId reqId, BindingResult result)  {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getCategoryId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameType()) {
            reqId.setGameType((byte)0);
        }
        return gameEventService.gameCategoryAndSystem(reqId.getCategoryId(), reqId.getGameType());
    }

    /**
     *  赛事编辑-----更新赛事的组数和晋级队伍数目
     *
     * */
    @RequestMapping(value="/updateGroupNumAndRankNum", method = RequestMethod.POST)
    public APIResponse<?> updateGroupNumAndRankNum(@RequestBody ReqGroupNumAndRankNum reqGroupNumAndRankNum, BindingResult result) throws Exception {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.updateGroupNumAndRankNum(reqGroupNumAndRankNum);
    }

    /**
     * 赛事编辑-----赛事管理详情
     *
     * */
    @RequestMapping(value="/gameRoundAndGameTeam", method = RequestMethod.POST)
    public APIResponse<?> gameRoundAndGameTeam(@RequestBody ReqId reqId, BindingResult result) throws Exception {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.gameRoundAndGameTeam(reqId.getGameEventId());
    }


    /**
     * 赛事编辑----赛事列表展示
     *
     * 备注:由于版本原因,2.6版本以后开始控制整个接口
     *
     *
     * */
    @RequestMapping(value="/gameEventList", method = RequestMethod.POST)
    public APIResponse<PageObj<GameEvent>> clubGameEventList(@RequestBody ReqIdAndPage reqIdAndPage, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqIdAndPage.getClubId()) {
            return APIResponse.returnFail("clubId不能为空");
        }
        if (!versionControlService.isNewestVer(reqIdAndPage.getPlatformType(), reqIdAndPage.getVer())){
            return APIResponse.returnFail("请前往AppStore或应用商店升级到最新版本");
        }else {
            return gameEventService.clubGameEventList(reqIdAndPage.getClubId(), reqIdAndPage.getPageNo(), reqIdAndPage.getPageSize(), (Integer) request.getAttribute("uid"));
        }
    }

    /**
     * 赛事编辑---获取积分赛报名队伍信息
     *
     * */
    @RequestMapping(value="/gameTeamByGameEventId", method = RequestMethod.POST)
    public APIResponse<PageObj<List<GameTeam>>> gameTeamByGameEventId(@RequestBody ReqIdAndPage reqIdAndPage, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqIdAndPage.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(gameEventService.gameTeamByGameEventId(reqIdAndPage.getGameEventId(), reqIdAndPage.getPageNo(), reqIdAndPage.getPageSize()));
    }

    /**
     * 赛事展示---赛事详情展示(官方赛事:包涵积分赛和非积分赛)
     *
     * */
    @RequestMapping(value="/gameEventDetail", method = RequestMethod.POST)
    public APIResponse<RespGameEventDetail> gameEventDetail(@RequestBody ReqId reqId, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(clubGameEventConfigService.gameEventDetail(reqId.getGameEventId(), (Integer) request.getAttribute("uid")));
    }


    /**
     * 赛事展示---获取赛程轮次列表
     *
     * */
    @RequestMapping(value="/gameGroundList", method = RequestMethod.POST)
    public APIResponse<List<GameRound>> gameGroundList(@RequestBody ReqId reqId, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = 0;
        if (!StringUtils.isEmpty(request.getAttribute("uid"))) {
            uid = (Integer) request.getAttribute("uid");
        }
        return gameEventService.gameGroundList(reqId.getGameEventId(), uid);
    }

    /**
     * 赛事展示---获取赛程轮次下赛程列表
     *
     * */
    @RequestMapping(value="/gameScheduleList", method = RequestMethod.POST)
    public APIResponse<List<GameSchedule>> gameScheduleList(@RequestBody ReqId reqId, BindingResult result)  {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameRoundId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return APIResponse.returnSuccess(gameEventService.gameSchedule(reqId.getGameRoundId(), reqId.getGameEventId()));
    }

    /**
     * 赛事展示---获取赛事数据展示
     *
     * */
    @RequestMapping(value="/gameEventData", method = RequestMethod.POST)
    public APIResponse<RespGameEventData> gameEventData(@RequestBody ReqIdAndPage reqIdAndPage, BindingResult result)  {
        if (null == reqIdAndPage.getGameEventId()) {
            return APIResponse.returnFail("gameEventId 不能为空");
        }

        Integer pageSize = null == reqIdAndPage.getPageSize() ?  10 : reqIdAndPage.getPageSize();

        RespGameEventData respGameEventData = gameEventService.gameEventData(reqIdAndPage.getGameEventId(),reqIdAndPage.getPageNo(), pageSize);

        if (null == respGameEventData) {
            respGameEventData = new RespGameEventData();
        }

        return APIResponse.returnSuccess(respGameEventData);
    }

    /**
     * 赛事展示---获取某场馆的队伍积分排名
     *
     * */
    @RequestMapping(value="/placeScoreRankList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespOfficialGameTeam>>> placeScoreRankList(@RequestBody ReqIdAndPage reqIdAndPage, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqIdAndPage.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.placeScoreRankList(reqIdAndPage.getGameEventId(), reqIdAndPage.getPageNo(), reqIdAndPage.getPageSize());
    }

    /**
     * 赛事展示---获取某场馆下的积分赛事列表
     *
     * */
    @RequestMapping(value="/placeGameEventList", method = RequestMethod.POST)
    public APIResponse<?> placeGameEventList(@RequestBody ReqId reqId, BindingResult result) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getGameEventId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.placeGameEventList(reqId.getGameEventId());
    }

    /**
     * 赛事展示---搜索官方比赛,暂时只限制搜索 月度管内赛事
     *
     * */
    @RequestMapping(value="/searchGameEventList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespOfficialGameEvent>>> searchGameEventList(@RequestBody @Valid ReqSearchGameEvent reqSearchGameEvent, BindingResult result, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return gameEventService.searchGameEventList(reqSearchGameEvent, (Integer) request.getAttribute("uid"));
    }

    /**
     * 根据电话号码和赛事id,来判断该用户是否可以参加该赛事
     *
     * */
    @RequestMapping(value="/inviteUserByMobile", method = RequestMethod.POST)
    public APIResponse<?> searchGameEventList(@RequestBody ReqId reqId, HttpServletRequest request) throws InvocationTargetException, IllegalAccessException {
        if(null == reqId.getGameEventId()) {
            return APIResponse.returnFail("gameEventId不能为空");
        }
        if(StringUtil.isBlankOrNull(reqId.getInviteMobile())) {
            return APIResponse.returnFail("inviteMobile不能为空");
        }
        return gameEventService.inviteUserByMobile(reqId.getGameEventId(), reqId.getInviteMobile(), (Integer) request.getAttribute("uid"));
    }


    /**
     * 赛事编辑----创建俱乐部赛事
     *
     * */
    @RequestMapping(value="/createClubGameEvent", method = RequestMethod.POST)
    public APIResponse<?> createClubGameEvent(@RequestBody @Valid ReqClubCreateGameEvent reqClubCreateGameEvent, BindingResult result, HttpServletRequest request) throws Exception {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        //时间校验
        if (reqClubCreateGameEvent.getEndTime()<1) {
            return APIResponse.returnFail("结束时间不能为空");
        }
        if (reqClubCreateGameEvent.getStartTime()>=reqClubCreateGameEvent.getEndTime()) {
            return APIResponse.returnFail("时间参数不合法");
        }
        if (reqClubCreateGameEvent.getGameSystemType().intValue() == GameSystemConfigEnum.CONFIG_GAME_TYPE_MIXED.getId() && reqClubCreateGameEvent.getTeamLimitNum().intValue() > 128) {
            return APIResponse.returnFail("混合赛参赛人数不能超过128");
        }
        return APIResponse.returnSuccess(clubGameEventConfigService.createGameEvent(reqClubCreateGameEvent, Integer.parseInt(request.getAttribute("uid")+"")));
    }


    /**
     * 赛事查询配置
     * */
    @RequestMapping(value="/gameEventSearchConfig", method = RequestMethod.POST)
    public APIResponse<?> gameEventSearchConfig(@RequestBody @Valid ReqGameEventSearch repGameEventSearch) {

        try{
            int type = 0;
            if (ObjectUtil.isNotObjectEmpty(repGameEventSearch)) {
                type = repGameEventSearch.getType();
            }

            return APIResponse.returnSuccess(gameEventSearchService.gameEventSearchConfig(type));
        } catch (Exception e) {
            logger.error("gameEventSearchConfig is faild. e:", e);
            return APIResponse.returnFail("Server exception");
        }
    }

    /**
     * 赛事查询
     * */
    @RequestMapping(value="/gameEventSearch", method = RequestMethod.POST)
    public APIResponse<?> gameEventSearch(@RequestBody @Valid ReqGameEventSearch repGameEventSearch, HttpServletRequest request) {
        logger.info("gameEventSearch repGameEventSearch:"+ repGameEventSearch.toString());

        try{
            //    接口新版屏蔽
//                // 设置用户登录信息   需求搁置, 处理是否已报名逻辑
//               Integer uid = (Integer) request.getAttribute("uid");
//               repGameEventSearch.setUid(uid);
//
//            if (repGameEventSearch.getCityId() == 0) {
//                logger.info("gameEventSearch repGameEventSearch cityId value error.");
//                return APIResponse.returnFail("param cityId value error");
//            }
//
//            // 分页页面大小默认为10
//            if (repGameEventSearch.getPageSize() == 0) {
//                repGameEventSearch.setPageSize(10);
//            }

//            gameEventSearchService.gameEventSearchSequence(repGameEventSearch)
        }catch (Exception e) {
            logger.error("gameEventSearch is faild. e:", e);
            return APIResponse.returnFail("Server exception");
        }

        return APIResponse.returnSuccess(PageObj.create(0, repGameEventSearch.getPageNo(),repGameEventSearch.getPageSize(), Collections.emptyList()));
    }

    /**
     * 赛事查询
     * */
    @RequestMapping(value="/clubGameEvent", method = RequestMethod.POST)
    public APIResponse<?> clubGameEvent(@RequestBody @Valid ReqId reqId) throws InvocationTargetException, IllegalAccessException {

        if (null == reqId.getClubId()) {
            return APIResponse.returnFail("clubId不能为空");
        }
        return APIResponse.returnSuccess(gameEventService.getResGameEventsByClubId(reqId.getClubId()));
    }

    /**
     * 小球现场编排 —— 获取分项、赛制、等级
     */
    @RequestMapping(value="/gameCategoryAndSystemForLive", method = RequestMethod.POST)
    public APIResponse<RespGameSystemAndUserClubMsg> gameCategoryAndSystemForLive(@RequestBody ReqId reqId, BindingResult result, HttpServletRequest request) {
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        if (null == reqId.getCategoryId()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = (Integer)request.getAttribute("uid");

        return APIResponse.returnSuccess(gameEventService.getGameSystemAndUserClubMsg(reqId.getCategoryId(), uid));
    }

    /**
     * 小球现场编排 —— 创建现场编排
     */
    @RequestMapping(value="/createArrangeLiveGameEvent", method = RequestMethod.POST)
    public APIResponse<Integer> createArrangeLiveGameEvent(@RequestBody ReqCreateArrangeLiveGameEvent req,HttpServletRequest request,BindingResult result) throws Exception{
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = (Integer)request.getAttribute("uid");
        return APIResponse.returnSuccess(liveArrangeGameEventService.createGameEvent(uid, req).getId());
    }

    /**
     * 小球现场编排 —— 编排搭档
     *
     */
    @RequestMapping(value="/gameEventRound", method = RequestMethod.POST)
    public APIResponse<Integer> gameEventRound(@RequestBody ReqCreateArrangeLiveGameEvent req,HttpServletRequest request,BindingResult result) throws Exception{
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        Integer uid = (Integer)request.getAttribute("uid");
        req.setIsDouble(1);
        liveArrangeGameEventService.intiGameEventRound(uid, req);
        return APIResponse.returnSuccess();
    }


    /**
     * 小球现场编排 —— 获取编排详情
     */
    @RequestMapping(value="/liveGameEventDetailV2", method = RequestMethod.POST)
    public APIResponse<RespGameDetail> liveGameEventDetailV2(@RequestBody ReqId reqId,HttpServletRequest request ) throws Exception{
        if(null == reqId.getGameEventId() || reqId.getGameEventId() <= 0) {
            return APIResponse.returnFail("赛事id不存在");
        }
        Integer uid = (Integer)request.getAttribute("uid");
        RespGameDetail detail = liveArrangeGameEventService.liveGameEventDetailV2(reqId.getGameEventId(), uid);
        if (null == detail || null == detail.getId() || detail.getId() < 1) {
            return APIResponse.returnFail("赛事id不存在");
        }
        return APIResponse.returnSuccess(detail);
    }

    /**
     *  小球现场编排 —— 对战表
     *
     * */
    @RequestMapping(value="/liveGameScheduleV2", method = RequestMethod.POST)
    public APIResponse<List<RespGameRound>> liveGameScheduleV2(@RequestBody ReqId reqId,HttpServletRequest request ) throws Exception{
        if(null == reqId.getGameEventId() || reqId.getGameEventId() <= 0) {
            return APIResponse.returnFail("赛事id不存在");
        }
        return APIResponse.returnSuccess(liveArrangeGameEventService.liveGameSchedule(reqId.getGameEventId()));
    }

    /**
     *  小球现场编排 —— 用户个人数据页
     *
     * */
    @RequestMapping(value="/userFLScoreMsgV2", method = RequestMethod.POST)
    public APIResponse<RespUserFLScoreMsg> userFLScoreMsgV2(@RequestBody ReqId reqId,HttpServletRequest request ) throws Exception{
        if(null == reqId.getUid() || reqId.getUid() <= 0) {
            return APIResponse.returnFail("赛事id不存在");
        }
        Integer uid = reqId.getUid();
        return APIResponse.returnSuccess();
    }

    /**
     *  小球现场编排 —— 用户录入比分
     *
     * */
    @RequestMapping(value="/userRecordScoreV2", method = RequestMethod.POST)
    public APIResponse<RespUserFLScoreMsg> userRecordScoreV2(@RequestBody ReqRecordGameScheduleResult reqId, HttpServletRequest request ) throws Exception{

        Integer uid = (Integer)request.getAttribute("uid");

        if (StringUtils.isEmpty(reqId.getScores())) {
            return APIResponse.returnFail("没有录入比分");
        }

        int flag = gameEventService.userRecordScoreV2(uid, reqId);

        if(flag > 0) {
            return APIResponse.returnFail("比分录入失败");
        }

        return APIResponse.returnSuccess();
    }

    /**
     *  小球现场编排 —— 编辑对手
     *
     * */
    @RequestMapping(value="/updateOpponentV2", method = RequestMethod.POST)
    public APIResponse<T> updateOpponentV2(@RequestBody ReqUpdateOpponent reqUpdateOpponent, HttpServletRequest request ) throws Exception{

        logger.info("updateOpponentV2 reqUpdateOpponent:"+ reqUpdateOpponent);
        gameEventService.updateOpponentV2(reqUpdateOpponent);

        return APIResponse.returnSuccess();
    }


    /**
     *  小球现场编排 —— 开启下一轮
     */
    @RequestMapping(value="/openNextGameRoundV2", method = RequestMethod.POST)
    public APIResponse<List<RespGameSchedule>> openNextGameRoundV2(@RequestBody ReqId reqId,HttpServletRequest request ) throws Exception {
        if (null == reqId.getGameEventId() || reqId.getGameEventId() <= 0) {
            return APIResponse.returnFail("赛事id不存在");
        }
        if (null == reqId.getGameRoundId() || reqId.getGameRoundId() <= 0) {
            return APIResponse.returnFail("轮次id不存在");
        }
        Integer uid = reqId.getUid();
        return APIResponse.returnSuccess(liveArrangeGameEventService.openNextGameRoundV2(reqId.getGameEventId(), reqId.getGameRoundId(), uid));
    }

    /**
     * 排行榜
     */
    @RequestMapping(value="/getUserRank", method = RequestMethod.POST)
    public APIResponse<?> getUserRank(@RequestBody ReqId reqId, BindingResult result, HttpServletRequest request)  {
        if(null == reqId.getGameEventId()) {
            return APIResponse.returnFail("赛事id不能为空");
        }
        return APIResponse.returnSuccess(liveArrangeGameEventService.getUserScoreRank(reqId.getGameEventId()));
    }
}
